﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Diagnostics;


namespace SPThumbnailExtender.Interne
{
    internal class BmpWebBrowser
    {
        public Uri Url;
        public Action<Bitmap> ResultDelegate;
    } 



    internal class ThumbnailHtml : ThumbnailBase
    {

        protected void BitmapRendered(Bitmap result)
        {
            Helper.DeleteThumbnail(this.FileUri, 0);
            Helper.InsertThumbnail(this.FileUri, 0, result);
        }   
        public ThumbnailHtml(Uri uri):base(uri)
        {
            this.Width = 1280;
            this.Height = 1024;
        }
        public ThumbnailHtml(Uri uri, int Width, int Height)
            : base(uri, Width, Height)
        { }

        static readonly object _lock = new object();
        public override System.Drawing.Bitmap GenerateThumbnail()
        {
            return GenerateThumbnail(0);
        }

        public override System.Drawing.Bitmap GenerateThumbnail(int Page)
        {
            BmpWebBrowser bwr = new BmpWebBrowser();
            bwr.Url = this.FileUri;
            bwr.ResultDelegate = BitmapRendered;
            ParameterizedThreadStart ts = new ParameterizedThreadStart(this.DoWork);
            Thread t = new Thread(ts);
            t.SetApartmentState(ApartmentState.STA);
            t.Start(bwr);
            
            return null;

        }
        private void DoWork(object bwr)
        {
            Bitmap bmp = null;
            BmpWebBrowser bwb = bwr as BmpWebBrowser;
            if (bwb != null)
                bmp = ProcessHtml(bwb);
            bwb.ResultDelegate(bmp);
        }

        private Bitmap ProcessHtml(BmpWebBrowser bwb)
        {
            Bitmap bitmap = new Bitmap(this.Width, this.Height);
            lock (_lock)
            {
                using (WebBrowser browser = new WebBrowser
                {
                    ScrollBarsEnabled = false,
                    Size = new Size(this.Width, this.Height),
                    ScriptErrorsSuppressed = true
                })
                {

                    browser.NewWindow += new CancelEventHandler(wb_NewWindow);
                    browser.Navigate(bwb.Url);
                    
                    Stopwatch sw = Stopwatch.StartNew();
                    while (browser.ReadyState != WebBrowserReadyState.Complete)
                    {
                        Application.DoEvents();
                        Thread.Sleep(5000);
                        //if (sw.ElapsedMilliseconds <= 10000) continue;
                        //break;
                    }
                    Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
                    browser.DrawToBitmap(bitmap, rect);
                }
            }
            return bitmap;

        }
        void wb_NewWindow(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }

        public override void GenerateThumbnails()
        {
            GenerateThumbnail();
        }

        public override int GetCount()
        {
            return 1;
        }
    }
}
